/*
Copyright [2023] [fuwen_soft]
*/
package vip.xiaonuo.sys.modular.sysappinfofeatures.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import vip.xiaonuo.core.consts.CommonConstant;
import vip.xiaonuo.core.enums.CommonStatusEnum;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.util.PoiUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.sys.modular.sysappinfofeatures.entity.SysAppInfoFeatures;
import vip.xiaonuo.sys.modular.sysappinfofeatures.enums.SysAppInfoFeaturesExceptionEnum;
import vip.xiaonuo.sys.modular.sysappinfofeatures.mapper.SysAppInfoFeaturesMapper;
import vip.xiaonuo.sys.modular.sysappinfofeatures.param.SysAppInfoFeaturesParam;
import vip.xiaonuo.sys.modular.sysappinfofeatures.service.SysAppInfoFeaturesService;

import javax.annotation.Resource;
import java.util.List;

/**
 * APP特色内容service接口实现类
 *
 * @author yanwei
 * @date 2023-03-21 12:07:26
 */
@Service
public class SysAppInfoFeaturesServiceImpl extends ServiceImpl<SysAppInfoFeaturesMapper, SysAppInfoFeatures> implements SysAppInfoFeaturesService {

    @Override
    public PageResult<SysAppInfoFeatures> page(SysAppInfoFeaturesParam sysAppInfoFeaturesParam) {
        QueryWrapper<SysAppInfoFeatures> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(sysAppInfoFeaturesParam)) {

            // 根据特色标题 查询
            if (ObjectUtil.isNotEmpty(sysAppInfoFeaturesParam.getFeaturesTitle())) {
                queryWrapper.lambda().eq(SysAppInfoFeatures::getFeaturesTitle, sysAppInfoFeaturesParam.getFeaturesTitle());
            }
            // 根据特色内容 查询
            if (ObjectUtil.isNotEmpty(sysAppInfoFeaturesParam.getFeaturesInfo())) {
                queryWrapper.lambda().eq(SysAppInfoFeatures::getFeaturesInfo, sysAppInfoFeaturesParam.getFeaturesInfo());
            }
            // 根据特色ICON 查询
            if (ObjectUtil.isNotEmpty(sysAppInfoFeaturesParam.getFeaturesIcon())) {
                queryWrapper.lambda().eq(SysAppInfoFeatures::getFeaturesIcon, sysAppInfoFeaturesParam.getFeaturesIcon());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<SysAppInfoFeatures> list(SysAppInfoFeaturesParam sysAppInfoFeaturesParam) {
        return this.list();
    }

    @Override
    public void add(SysAppInfoFeaturesParam sysAppInfoFeaturesParam) {
        SysAppInfoFeatures sysAppInfoFeatures = new SysAppInfoFeatures();
        BeanUtil.copyProperties(sysAppInfoFeaturesParam, sysAppInfoFeatures);
        this.save(sysAppInfoFeatures);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SysAppInfoFeaturesParam> sysAppInfoFeaturesParamList) {
        sysAppInfoFeaturesParamList.forEach(sysAppInfoFeaturesParam -> {
            this.removeById(sysAppInfoFeaturesParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SysAppInfoFeaturesParam sysAppInfoFeaturesParam) {
        SysAppInfoFeatures sysAppInfoFeatures = this.querySysAppInfoFeatures(sysAppInfoFeaturesParam);
        BeanUtil.copyProperties(sysAppInfoFeaturesParam, sysAppInfoFeatures);
        this.updateById(sysAppInfoFeatures);
    }

    @Override
    public SysAppInfoFeatures detail(SysAppInfoFeaturesParam sysAppInfoFeaturesParam) {
        return this.querySysAppInfoFeatures(sysAppInfoFeaturesParam);
    }

    /**
     * 获取APP特色内容
     *
     * @author yanwei
     * @date 2023-03-21 12:07:26
     */
    private SysAppInfoFeatures querySysAppInfoFeatures(SysAppInfoFeaturesParam sysAppInfoFeaturesParam) {
        SysAppInfoFeatures sysAppInfoFeatures = this.getById(sysAppInfoFeaturesParam.getId());
        if (ObjectUtil.isNull(sysAppInfoFeatures)) {
            throw new ServiceException(SysAppInfoFeaturesExceptionEnum.NOT_EXIST);
        }
        return sysAppInfoFeatures;
    }

    @Override
    public void export(SysAppInfoFeaturesParam sysAppInfoFeaturesParam) {
        List<SysAppInfoFeatures> list = this.list(sysAppInfoFeaturesParam);
        PoiUtil.exportExcelWithStream("SnowySysAppInfoFeatures.xls", SysAppInfoFeatures.class, list);
    }

}
